#ifndef CLIENTNETSYSTEM_H
#define CLIENTNETSYSTEM_H

#include <QObject>

class seven::TransferMessage;
class FileDB;
class ClientConnection;

class ClientNetsystem : public QObject
{
	Q_OBJECT

public:
	ClientNetsystem(QObject *parent);
	~ClientNetsystem();

	void Start();
	void Connect(const QString& host, const QString& port);

protected:
	enum ClientState
	{
		NONE,
		START,
		CONNECT_PENDING,
		CONNECTED,
		PATH_TABLE_SENDING,
		PATH_TABLE_FINISHED,
		FILE_TABLE_SENDING,
		FILE_TABLE_FINISHED,
		MATCH_RESULT_FINISHED
	};

private slots:
	void HandleMsg(const seven::TransferMessage& msg);

protected:
	void OnPATH_TABLE_FINISHED(const seven::TransferMessage& msg);
	void OnFILE_TABLE_FINISHED(const seven::TransferMessage& msg);
	void OnCONNECTED(const seven::TransferMessage& msg);
	void OnFILE_CONTENT_REQ(const seven::TransferMessage& msg);

protected:
	bool SendTransferMsg(seven::TransferMessage& msg);
	bool SendPathTable(FileDB* pDB);
	bool SendFileTable(FileDB* pDB);

protected:
	ClientState GetState() {return state;}
	void SetState(ClientState s) { state = s;}

	FileDB* GetFileDB() {return fileDB.data();}
	void Clear();
protected:
	ClientConnection * tcpSocket;
	qint64 messageSize;
	ClientState state;
	QSharedPointer<FileDB> fileDB;
	
};

#endif // CLIENTNETSYSTEM_H
